package com.android.RssReader.Screen;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.Provider.NewsDatabaseHelper;
import com.android.RssReader.Service.ServiceManager;
import com.android.RssReader.Utils.AsyncImageLoader;
import com.android.RssReader.Utils.AsyncImageLoader.ImageCallback;


public class ScreenNewsContent  extends Screen{
	private ScreenArgs screenArgs;
	private TextView categoryTv, titleTv, dateTv, contentTv;
	private ImageView image;
	private CheckBox favoritesBtn;
	private AsyncImageLoader asyncImageLoader;
	private String imageUrl;
	private ScrollView scrollView;
	public ScreenNewsContent() {
		super(SCREEN_TYPE.SCREEN_NEWS_T, ScreenNewsContent.class.getCanonicalName());
		asyncImageLoader = new AsyncImageLoader();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_news_content);
		RssReaderApplication.LogD(ScreenNewsContent.class, "onCreate");
		screenArgs = (ScreenArgs) getIntent().getSerializableExtra("args");
		scrollView = (ScrollView) findViewById(R.id.ScrollView);
		categoryTv = (TextView) findViewById(R.id.categoty_title_tv);
		titleTv = (TextView) findViewById(R.id.screen_news_title);
		dateTv = (TextView) findViewById(R.id.screen_news_date);
		contentTv = (TextView) findViewById(R.id.screen_news_content);
		image = (ImageView) findViewById(R.id.screen_news_image);
		categoryTv.setText((String)screenArgs.getExtra("category"));
		titleTv.setText((String)screenArgs.getExtra("title"));
		dateTv.setText((String)screenArgs.getExtra("date"));
		contentTv.setText((String)screenArgs.getExtra("description"));
		imageUrl = "http://coolthink.vh2.in800.net/images/201007/goods_img/88_G_1278999498433.jpg";
		Drawable cachedImage = asyncImageLoader.loadDrawable(
				imageUrl, image, new ImageCallback() {
					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView.setBackgroundDrawable(imageDrawable);
					}
				});
		if (cachedImage == null) {
			image.setBackgroundResource(R.drawable.icon);
		} else {
			image.setBackgroundDrawable(cachedImage);
		}
		
		favoritesBtn = (CheckBox) findViewById(R.id.favorites_btn);
		favoritesBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
		@Override 
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
		if (buttonView.isChecked()) { 
			Toast.makeText(ScreenNewsContent.this, "isChecked:", Toast.LENGTH_SHORT).show();
			if(!ServiceManager.getNewsservice().getMediaDB().isInLocalFavoritesNews((String)screenArgs.getExtra("title"))){
				ContentValues contentValues = new ContentValues();
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_ID, (String)screenArgs.getExtra("title"));
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_TITLE, (String)screenArgs.getExtra("title"));
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_DESCRIPTION, (String)screenArgs.getExtra("description"));
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_LINK, (String)screenArgs.getExtra("link"));
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_DATE, (String)screenArgs.getExtra("date"));
				contentValues.put(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_CATEGORY, (String)screenArgs.getExtra("category"));
				ServiceManager.getNewsservice().getMediaDB().insertCountLocalFavoritesNews(contentValues);
			} else {
				RssReaderApplication.LogD(ScreenNewsContent.class, "插入重复数据:" + (String)screenArgs.getExtra("title"));
			}
		} 
		else 
		{
			Toast.makeText(ScreenNewsContent.this, "notChecked:", Toast.LENGTH_SHORT).show();
			if(ServiceManager.getNewsservice().getMediaDB().isInLocalFavoritesNews((String)screenArgs.getExtra("title"))){
				RssReaderApplication.LogD(ScreenNewsContent.class, "notChecked:" + screenArgs.getExtra("item_id"));
				ServiceManager.getNewsservice().getMediaDB().deleteCountLocalFavorites((Long)screenArgs.getExtra("item_id"));
			}
		}}});

	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		RssReaderApplication.LogD(ScreenNewsContent.class, "onNewIntent");
		screenArgs = (ScreenArgs) intent.getSerializableExtra("args");
		categoryTv.setText((String)screenArgs.getExtra("category"));
		titleTv.setText((String)screenArgs.getExtra("title"));
		dateTv.setText((String)screenArgs.getExtra("date"));
		contentTv.setText((String)screenArgs.getExtra("description"));
		scrollView.fullScroll(View.FOCUS_UP);
	}

	@Override
	protected void onPause() {
		super.onPause();
		RssReaderApplication.LogD(ScreenNewsContent.class, "onPause");
		
	}

	@Override
	protected void onResume() {
		super.onResume();
//		favoritesBtn.setChecked(false);
//		if(ServiceManager.getNewsservice().getMediaDB().isInLocalFavoritesNews((String)screenArgs.getExtra("title"))){
//			favoritesBtn.setChecked(true);
//		}
		RssReaderApplication.LogD(ScreenNewsContent.class, "onResume");
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		RssReaderApplication.LogD(ScreenNewsContent.class, "onStart");
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		RssReaderApplication.LogD(ScreenNewsContent.class, "onStop");
	}

}
